package com.example.testapplication.recyclerUtils

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.testapplication.R

abstract class BaseAdapter<K>(c: Context, id: Int, list: List<K>?=null) :
    RecyclerView.Adapter<BaseViewHolder<K>>() {

    private var layoutId: Int
    private var data = mutableListOf<K>()
    private var context: Context

    init {
        layoutId = id
        if (list != null) {
            data.addAll(list)
        }
        context = c
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<K> {
        val item = LayoutInflater.from(context).inflate(layoutId, null)
        return BaseViewHolder(item)
    }

    override fun onBindViewHolder(holder: BaseViewHolder<K>, position: Int) {
        holder.item = data[position]
        onBindViewHolder(holder)
    }

    abstract fun onBindViewHolder(holder: BaseViewHolder<K>)

    override fun getItemCount(): Int {
        return data.size
    }

    fun getData(): MutableList<K> {
        return data
    }

}